package com.cseg674.dataentry.javaparser;

import java.io.*;
import java.util.*;
import org.apache.commons.fileupload.FileItem;

/**
 * DEApplication class used as Entry Point for Servlet; executes out Data Entry Application
 * Updated on 03/21/09 to support Web Services configuration
 * 
 * @author Hassan
 *
 */
public class DEApplication {
	private static final String PROJECT   = "default";		// default project name
	private static final String OUTPUTXML = "output.xml";	// default output name
		
    /**
     * Default CTOR: Creates a new instance of DEApplication
     */
    public DEApplication() {}
       
    /**
     * Initiates the parsing process for all input files
     * @param allFiles ArrayList of uploaded files as JAVA File objects
     * @return the RdfTree object containing source information
     */
    public static RdfTree beginParse(ArrayList<FileItem> allFiles) {
		JavaParser parser;
		int numberOfFiles = allFiles.size();
		if (numberOfFiles == 0) {
		//	System.out.println("No Files Available!  Exiting...");
			System.exit(0);
		}
		int i = 0;
		try {
			while (i < numberOfFiles) {
				try {
					parser = new JavaParser(allFiles.get(i).getInputStream());
					parser.CompilationUnit();
				} catch (java.io.FileNotFoundException e) {
				//	System.out.println("File " + allFiles.get(i) + " not found.");
					return null;
				} catch (ParseException e) {
				//	System.out.println(e.getMessage());
				//	System.out.println("Java Parser Version 1.1:  Encountered errors during parse.");
				} catch (Exception e){ }

				i++;
			}

		}

		catch (ArrayIndexOutOfBoundsException e) {
		//	System.out.println("You Entered A wrong Number Of Params: Need "
		//			+ numberOfFiles
		//			+ " files but fewer file names were provided");
			System.exit(1);
		}
		//System.out.println("Parser finished populating tree");
		return JavaParser.getTree();
	}

    /**
     * Entry point for Data Entry Application - called by DE Servlet
     * This method will carry out the parsing and RDF processing 
     * for the input files
     * @param uploadedFiles ArrayList of uploaded files as JAVA File objects
     * @return the xml file as a JAVA File object
     */
    public static File parseFiles(ArrayList<FileItem> uploadedFiles) {
		File outputFile;	// output file object to return to Servlet

		// parse input files and create RDFtree
		RdfTree tree = beginParse(uploadedFiles);

		// process tree into RDF/XML file
		if (tree != null) {
		//	System.out.println("Converting tree to RDF!");
			outputRDF(tree);			
		} else {
		//	System.out.println("Null Tree!");
		}

		// get File object and return to calling Servlet
		try {
			outputFile = new File(getXMLName());
		} catch (java.lang.NullPointerException e) {
		//	System.out.println("Output file is null.");
			return null;
		}

		return outputFile;
	} 
    
    /**
     * Test package functionality
     * 
	 * @param args the command line arguments
	 */
/*    public static void main(String[] args) {

		// test return file
		File xmlOut;

		// test input files
		File a1 = new File("test/NewJPanel.java");
		File a2 = new File("test/NumberButton.java");
		File a3 = new File("test/NumberConverter.java");
		File a4 = new File("test/NumberConverterBeanInfo.java");
		File a5 = new File("test/StackInterface.java ");
		File a6 = new File("test/MyRoundRect.java");
		File a7 = new File("test/MyShape.java");

		// add test files to array list
		ArrayList<File> testAL = new ArrayList();
		testAL.add(a1);
		testAL.add(a2);
		testAL.add(a3);
		testAL.add(a4);
		testAL.add(a5);
		testAL.add(a6);
		testAL.add(a7);

		// check contents of array list
		System.out.println("ArrayList contents: ");
		for (int i = 0; i < testAL.size(); ++i) {
			System.out.println(testAL.get(i));
		}

		// test new method, verify output
		xmlOut = parseFiles(testAL);
		System.out.print("Output XML file: ");
		System.out.println(xmlOut);
	}
*/
    /**
     * Perform conversion of tree into and RDF/XML file
     * 
     * @param tree parsed source files in a tree format
     */
    private static void outputRDF(RdfTree tree) {
		SourceOntology ontology = new SourceOntology("");
		if (ontology.Initialize() == false) {
		//	System.out.println("Error initializing source ontology");
			System.exit(1);
		}
		new TreeToRdf().ToRdf(tree, ontology);
		//System.out.println("XML file: " + getXMLName());
	}

    /**
     * Get current project name
     * 
     * @return name of current project
     */
	public static String getProject() {
		return PROJECT;
	}

	/**
     * Get output xml file name
     * 
     * @return name of output xml file
     */
	public static String getXMLName() {
		return OUTPUTXML;
	}
}
